package com.systop.asset.entity;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.systop.asset.enums.HttpCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 *
 * 同一响应格式
 *
 * **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RestResponse<T> {

    private int code;
    private T data;
    private String message;


    public static <T>  RestResponse<T> success(T data) {
        return new RestResponse<>(200, data, "success");
    }

    public static <T>  RestResponse<T> success() {
        return success(null);
    }

    public static <T> RestResponse<T> failure(int code, String message) {
        return new RestResponse<>(code, null, message);
    }

    public static <T> RestResponse<T> failure(int code, String message, T data) {
        return new RestResponse<>(code, data, message);
    }

    public static <T> RestResponse<T> failure(HttpCodeEnum httpCodeEnum) {
        return new RestResponse<>(httpCodeEnum.getCode(), null, httpCodeEnum.getMsg());
    }

    public String asJsonString(){
        return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
    }

}
